<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta content="yes" name="apple-mobile-web-app-capable">
  <meta content="yes" name="apple-touch-fullscreen">
  <meta content="telephone=no,email=no" name="format-detection">
  <meta name="viewport" content="width=device-width, height=device-height, viewport-fit=cover, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>萤火钱包DApp示例</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css" crossorigin="anonymous" />
  <script src="https://cdn.bootcss.com/vConsole/3.2.0/vconsole.min.js"></script>
  <style>
    .loading{
      position: fixed;
      top: 0;
      bottom: 0;
      left:0;
      right: 0;
      display: flex;
    }
    .loading-mask{
      position: fixed;
      top: 0;
      bottom: 0;
      left:0;
      right: 0;
      background: #333333;
      opacity: 0.6;
    }
    .loading-txt{
      position: fixed;
      z-index: 99;
      color: #FFFFFF;
      text-align: center;
      z-index: 99;
      padding-top: 20%;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
    }
    .code{
      margin: 5px 0;
      padding: 5px 5px;
      background: #e3e3e3;
      font-size: 12px;
      overflow: auto;
    }
    .primary-btn{
      padding: 8px 8px;
      background: #21CE90;
      color:#FFFFFF;
      border-width: 0px; /* 边框宽度 */
      border-radius: 3px; /* 边框半径 */
      cursor: pointer; /* 鼠标移入按钮范围时出现手势 */
      outline: none; /* 不显示轮廓线 */

    }
  </style>
  <script>
    var vc = new VConsole();
  </script>
  
</head>
<body>
  <div id="props">
    版本号 window.FFW.version:<span id="prop-version"></span><br/>
    运行平台 window.FFW.platform:<span id="prop-platform"></span><br/>
    账户公钥 window.FFW.address:<span id="prop-address"></span><br/>
    联系人 window.FFW.contacts:<span id="prop-contacts"></span><br/>
    设备UUID window.FFW.uuid:<span id="prop-uuid"></span><br/>
    语言 window.FFW.locale:<span id="prop-locale"></span><br/>
  </div>
  <hr/>
  <div id="btns">
    <button id="btn_balances" class="primary-btn">获取账户余额</button><br/>
    <div class="code">
      <pre>
window.FFW.balances(function(response){
  if(response.code === 'fail'){
    console.log('error:' + response.message)  
  }else{
    console.log('query for balances successfully')
    console.log(response.data)
  }
})
      </pre>
    </div>
		<div>账户余额:<span id="balances_data"></span></div>
    <br/>
    <hr/>
    <br/>

    <button id="btn_pay" class="primary-btn">支付1XFF</button><br/>
    <button id="btn_pay_id" class="primary-btn">支付1XFF(memo_type:text,内容为12345)</button><br/>
    <div class="code">
      <pre>
FFW.pay({
  destination: 'GCKKUWHT3ILQWWKQ3MUOCAC7LRJNLCOES7SEI6TCQVGZD4GCULO2PGNU', 
  code: 'XFF', 
  issuer: 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF', 
  amount: 1, 
  memo_type: 'TEXT', //取值见stellar的api文档
  memo: 'Hello,FFW'
}, function(response){
    if(response.code === 'fail'){
      console.log('error:' + response.message)  
      alert('支付失败！'+response.message)
    }else{
      console.log('pay success')
      alert('支付成功')
    }
});
      </pre>
    </div>
    <br/>
    <hr/>
    <br/>
    <button id="btn_pathpay" class="primary-btn">路径支付1XFF</button><br/>
    <div class="code">
        <pre>
FFW.pathPayment({
    destination: 'GCKKUWHT3ILQWWKQ3MUOCAC7LRJNLCOES7SEI6TCQVGZD4GCULO2PGNU', 
    code: 'XFF', 
    issuer: 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF', 
    amount: 1, 
    memo_type: 'TEXT', 
    memo: 'Hello,FFW'
  }, function(response){
      if(response.code === 'fail'){
        console.log('error:' + response.message)  
      }else{
        console.log('success')
      }
  })
        </pre>
      </div>
      <br/>
      <hr/>
      <br/>
    <button id="btn_sign" class="primary-btn">签名数据</button><br/>
    
    <div>待签名的数据:<span id="sign_data"></span></div>
    <div>签名后的结果:<span id="sign_result_data"></span></div>
    <div class="code">
        <pre>
let data = { address: window.FFW.address,time: new Date().getTime() }
data = JSON.stringify(data)
$("#sign_data").text(data)
window.FFW.sign(data, function(response){
  if(response.code === 'fail'){
      console.log('error:' + response.message)  
    }else{
      console.log('success')
      console.log(`签名后的数据：${response.data}`)
    }
});
        </pre>
      </div>

    <br/>
    <hr/>
    <br/>


    <button id="btn_signXDR" class="primary-btn">XDR签名</button><br/>
    <div>待签名的数据:<span id="signxdr_data"></span></div>
    <div>签名后的结果:<span id="signxdr_result_data"></span></div>
    <div class="code">
        <pre>
var xdr = 'AAAAAEpng8wi7nIqz02/1bmC4I5jzz763WoadKIWy7M5MVc3AAAAZACHjkkAAAABAAAAAAAAAAAAAAABAAAAAAAAAAoAAAALaG9tZV9kb21haW4AAAAAAQAAABBodHRwOi8vZmNoYWluLmlvAAAAAAAAAAA='
$("#signxdr_data").text(xdr);
window.FFW.signXDR(xdr, "签名XDR", function(response){
    alert('sign---xdr----result ---' + JSON.stringify(response))
    if(response.code === 'fail'){
      console.log('error:' + response.message)  
      alert('签名失败！'+response.message)
    }else{
      console.log('success')
      alert('签名成功,数据可以直接提交到Horizon')
      $("#signxdr_result_data").text(response.data)
    }
})
        </pre>
      </div>

    <br/>
    <hr/>
    <br/>


    <button id="btn_trust" class="primary-btn">授信XFF</button><br/>
    <div class="code">
        <pre>
var code = 'XFF';
var issuer = 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF';
window.FFW.trust(code,issuer,function(response){
    if(response.code === 'fail'){
      console.log('error:' + response.message)  
      alert('授信失败')
    }else{
      console.log('trust success')
      alert('授信成功')
    }
});
    </pre>
    </div>
    <br/><hr/>
    <br/>

    <button id="btn_scan" class="primary-btn">扫描二维码</button><br/>
    <div class="code">
        <pre>
    window.FFW.scan(function(response){
      if(response.code === 'fail'){
        console.log('error:' + response.message)  
        alert('扫描失败！')
      }else{
        console.log('scan result:' + response.data)//response.data是二维码的扫描结果，字符串类型
        alert('扫描二维码结果：' + response.data )
      }
    });
    </pre>
    </div>
    <br/>
    <hr/>
    <br/>
    <button id="btn_share" class="primary-btn">分享图片</button><br/>
    <div class="code">
        <pre>
    var options = {
      message: '分享图片',
      files: ['base64格式图片']
    };
    window.FFW.share(options,function(response){
      if(response.code === 'fail'){
        console.log('error:' + response.message)  
        alert('分享失败！')
      }else{
        console.log('share success')
        alert('分享成功！')
      }
   });
   </pre>
   </div>


    <br/>
    <br/>
  </div>
  <div id="loading" class="loading">
    <div class="loading-mask"></div>
    <div class="loading-txt">正在加载...</div>
  </div>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
  <script>

    $(function(){
      
      load()
      $('#btn_pay').click(pay)
      $('#btn_pay_id').click(pay_id)
      $('#btn_pathpay').click(pathPay)
      $('#btn_sign').click(sign)
      $('#btn_signXDR').click(signXDR)
      $('#btn_trust').click(trust)
      $('#btn_scan').click(scan)
      $('#btn_share').click(share)
      $('#btn_balances').click(balances)

      //获取当前账户信息
      function load(){
        var interval = setInterval(function(){
          if(window.FFW){
            $("#prop-version").text(window.FFW.version)
            $("#prop-platform").text(window.FFW.platform)
            $("#prop-address").text(window.FFW.address)
            $("#prop-contacts").text(JSON.stringify(window.FFW.contacts))
            $("#prop-uuid").text(window.FFW.uuid)
            $("#prop-locale").text(window.FFW.locale)
            $("#loading").hide()
            clearInterval(interval);
          } 
        },3000);
      }

      function balances(){
      	console.log('------------------调用查询余额功能-----------')
      	FFW.balances(function(response){
      		if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('查询失败！'+response.message)
           }else{
             console.log('query for balances successfully')
             $("#balances_data").text(JSON.stringify(response.data))
             alert('查询余额成功')
           }
      	});
      };

      function pay(){
        console.log('-----调用支付功能----')
        FFW.pay({
         destination: 'GCKKUWHT3ILQWWKQ3MUOCAC7LRJNLCOES7SEI6TCQVGZD4GCULO2PGNU', 
         code: 'XFF', 
         issuer: 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF', 
         amount: 1, 
         memo_type: 'TEXT', //取值见stellar的api文档
         memo: 'Hello,FFW'
       }, function(response){
           if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('支付失败！'+response.message)
           }else{
             console.log('pay success')
             alert('支付成功')
           }
       });
      }
      
      function pay_id(){
        console.log('-----调用支付功能----')
        FFW.pay({
         destination: 'GCKKUWHT3ILQWWKQ3MUOCAC7LRJNLCOES7SEI6TCQVGZD4GCULO2PGNU', 
         code: 'XFF', 
         issuer: 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF', 
         amount: 1, 
         memo_type: 'TEXT', //取值见stellar的api文档
         memo: '12345'
       }, function(response){
           if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('支付失败！'+response.message)
           }else{
             console.log('pay success')
             alert('支付成功')
           }
       });
      }

      function pathPay(){
        FFW.pathPayment({
          destination: 'GCKKUWHT3ILQWWKQ3MUOCAC7LRJNLCOES7SEI6TCQVGZD4GCULO2PGNU', 
          code: 'XFF', 
          issuer: 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF', 
          amount: 1, 
          memo_type: 'TEXT', 
          memo: 'Hello,FFW'
        }, function(response){
           if(response.code === 'fail'){
             console.log('error:' + response.message)  
           }else{
             console.log('success')
           }
       })
      }

      function sign(){
        let data = { address: window.FFW.address,time: new Date().getTime() }
        data = JSON.stringify(data)
        $("#sign_data").text(data)
        window.FFW.sign(data, function(response){
          if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('数据签名失败！'+response.message)
           }else{
             console.log('success')
             alert('数据签名成功')
             $("#sign_result_data").text(response.data)
           }
        });
      }

      function signXDR(){
        var xdr = 'AAAAAEpng8wi7nIqz02/1bmC4I5jzz763WoadKIWy7M5MVc3AAAAZACHjkkAAAABAAAAAAAAAAAAAAABAAAAAAAAAAoAAAALaG9tZV9kb21haW4AAAAAAQAAABBodHRwOi8vZmNoYWluLmlvAAAAAAAAAAA='
        $("#signxdr_data").text(xdr);
        window.FFW.signXDR(xdr, "签名XDR", function(response){
            alert('sign---xdr----result ---' + JSON.stringify(response))
            if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('签名失败！'+response.message)
           }else{
             console.log('success')
             alert('签名成功,数据可以直接提交到Horizon')
             $("#signxdr_result_data").text(response.data)
           }
        })
      }

      function trust(){
        var code = 'XFF';
        var issuer = 'GAZEX2USUBMMWFRZFS77VDJYXUFLXI4ZGFPWX6TBNZCSTEQWNLFZMXFF';
        window.FFW.trust(code,issuer,function(response){
           if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('授信失败')
           }else{
             console.log('trust success')
             alert('授信成功')
           }
        })
      }

      function scan(){
        window.FFW.scan(function(response){
          if(response.code === 'fail'){
            console.log('error:' + response.message)  
            alert('扫描失败！')
          }else{
            console.log('scan result:' + response.data)//response.data是二维码的扫描结果，字符串类型
            alert('扫描二维码结果：' + response.data )
          }
        })
      }

      function share(){
        var options = {
         message: '分享图片',
         files: [''], //图片，支持远程图片或base64格式的图片等
        };
        window.FFW.share(options,function(response){
           if(response.code === 'fail'){
             console.log('error:' + response.message)  
             alert('分享失败！')
           }else{
             console.log('share success')
             alert('分享成功！')
           }
        })
      }


    })

  </script>
</body>
</html>